﻿using System;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace QuickDev.Common.Utils
{
    public class ImageProcess
    {
        /// <summary>
        /// Resizes images to the specified size.
        /// </summary>
        /// <param name="fileStream">The file stream.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="finalPath">Where to save the resized image</param>
        /// <param name="maxWidth">Maximum Width</param>
        /// <param name="maxHeight">Maximum Height</param>
        public static void ResizeAndSave( Stream fileStream , string fileName , string finalPath , int maxWidth , int maxHeight , string caption )
        {
            using ( System.Drawing.Bitmap originalBMP = new System.Drawing.Bitmap( fileStream ) )
            {
                // Calculate the new image dimensions
                int width = originalBMP.Width; //actual width
                int height = originalBMP.Height; //actual height
                int widthDiff = ( width - maxWidth ); //how far off maxWidth?
                int heightDiff = ( height - maxHeight ); //how far off maxHeight?

                //figure out which dimension is further outside the max size
                bool doWidthResize = ( maxWidth > 0 && width > maxWidth && widthDiff > -1 && ( widthDiff > heightDiff || maxHeight.Equals( 0 ) ) );
                bool doHeightResize = ( maxHeight > 0 && height > maxHeight && heightDiff > -1 && ( heightDiff > widthDiff || maxWidth.Equals( 0 ) ) );

                //only resize if the image is bigger than the max or where image is square, and the diffs are the same
                if ( doWidthResize || doHeightResize || ( width.Equals( height ) && widthDiff.Equals( heightDiff ) ) )
                {
                    int iStart;
                    Decimal divider;
                    if ( doWidthResize )
                    {
                        iStart = width;
                        divider = Math.Abs( (Decimal)iStart / (Decimal)maxWidth );
                        width = maxWidth;
                        height = (int)Math.Round( (Decimal)( height / divider ) );
                    }
                    else
                    {
                        iStart = height;
                        divider = Math.Abs( (Decimal)iStart / (Decimal)maxHeight );
                        height = maxHeight;
                        width = (int)Math.Round( (Decimal)( width / divider ) );
                    }
                }

                using ( System.Drawing.Bitmap newBMP = new System.Drawing.Bitmap( originalBMP , width , height ) )
                {
                    using ( System.Drawing.Graphics oGraphics = System.Drawing.Graphics.FromImage( newBMP ) )
                    {
                        oGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        oGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        oGraphics.DrawImage( originalBMP , 0 , 0 , width , height );

                        if ( !String.IsNullOrEmpty( caption ) && width > 100 && height > 100 )
                        {
                            //设置矩形区域的位置和大小
                            Rectangle rect = new Rectangle( 0 , height - 10 - 30 , width , 30 );
                            //使填充矩形的颜色从红色到黄色渐变
                            LinearGradientBrush lBrush = new LinearGradientBrush( rect , Color.White , Color.Transparent , LinearGradientMode.Horizontal );
                            oGraphics.FillRectangle( lBrush , rect );

                            Font f = new Font( "Verdana" , 15 );
                            Brush b = new SolidBrush( Color.Black );
                            oGraphics.DrawString( caption , f , b , 10 , height - 10 - 30 );
                        }

                        newBMP.Save( string.Format( "{0}{1}x{2}_{3}" , finalPath , maxWidth , maxHeight , fileName ) );
                    }
                }
            }
        }

        /// <summary>
        /// Resizes images to the specified size.
        /// </summary>
        /// <param name="fileStream">The file stream.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="finalPath">Where to save the resized image</param>
        /// <param name="maxWidth">Maximum Width</param>
        /// <param name="maxHeight">Maximum Height</param>
        public static void ResizeAndSave( Stream fileStream , string fileName , string finalPath , int maxWidth , int maxHeight )
        {
            ResizeAndSave( fileStream , fileName , finalPath , maxWidth , maxHeight , String.Empty );
        }
    }
}
